package com.open.core.handler;

import com.open.core.entity.Command;
import com.open.core.common.Result;
import io.netty.channel.ChannelHandlerContext;

import static com.open.core.NettyServer.GROUPALL;
import static com.open.core.NettyServer.USERS;

public class ConnectionHandler {
    public static void excute(ChannelHandlerContext ctx, Command command){

        if(USERS.containsKey(command.getNickname())){
            ctx.channel().writeAndFlush(Result.fail("用户已经上线，请勿重复登录"));
            ctx.channel().disconnect();
            return;
        }

        // 给所有用户发这个请求
        GROUPALL.add(ctx.channel());
        USERS.put(command.getNickname(), ctx.channel());
        ctx.channel().writeAndFlush(Result.success("连接服务器成功"));
        System.out.println(command.getNickname()+"用户上线");
        GROUPALL.writeAndFlush(Result.success ("alluser", USERS.keySet().toString()));
    }
}
